﻿using System.Collections.Generic;
using System.Data;
using POS.BusinessEntities;

namespace POS.DataObjects
{
    public class DOContacto : DOBase
    {
        #region Declarations

        private const string getListSP = "ContactoGetList";
        private const string saveSP = "ContactoSave";
        
        #endregion

        #region Methods: Public

        public virtual IList<BEContacto> GetList(int empresaId, TipoContacto tipo, int referencia)
        {
            var result = new List<BEContacto>();
            var command = Db.GetStoredProcCommand(getListSP);
            Db.AddInParameter(command, "@empresaId", DbType.Int32, empresaId);
            Db.AddInParameter(command, "@tipo", DbType.String, TipoContactoToString(tipo));
            Db.AddInParameter(command, "@referencia", DbType.Int32, referencia);
            var data = Db.ExecuteDataSet(command);

            if (data.Tables.Count == 1)
            {
                foreach (DataRow row in data.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow<BEContacto>(row));
            }
            return result;
        }

        public virtual void Save(BEContacto entity, int empresaId, string user)
        {
            var command = Db.GetStoredProcCommand(saveSP);
            Db.AddInParameter(command, "@id", DbType.Int32, entity.Id);
            Db.AddInParameter(command, "@nombre", DbType.String, entity.Nombre);
            Db.AddInParameter(command, "@empresaId", DbType.Int32, empresaId);
            Db.AddInParameter(command, "@tipo", DbType.String, TipoContactoToString(entity.Tipo));
            Db.AddInParameter(command, "@referencia", DbType.Int32, entity.Referencia);
            Db.AddInParameter(command, "@puesto", DbType.String, entity.Puesto);
            Db.AddInParameter(command, "@email", DbType.String, entity.Email);
            Db.AddInParameter(command, "@telefono", DbType.String, entity.Telefono);
            Db.AddInParameter(command, "@celular", DbType.String, entity.Celular);
            Db.AddInParameter(command, "@notas", DbType.String, entity.Notas);
            Db.AddInParameter(command, "@user", DbType.String, user);
            Db.ExecuteNonQuery(command);
        }

        #endregion

        #region Methods: Non-Public

        private static string TipoContactoToString(TipoContacto tipo)
        {
            switch (tipo)
            {
                case TipoContacto.Clientes:
                    return "C";
                case TipoContacto.Proveedores:
                    return "P";
            }
            return "";
        }
      
        #endregion
    }
}
